import type { AxiosPromise } from 'axios'
import request from '@/utils/request'

export interface LoginParams {
  username: string
  password: string
  clientId: string
  grantType: string
  rememberMe: boolean
  tenantId: string
  code: string
  uuid: string
  type?: 'account'
}

/**
 * 验证码返回
 */
export interface VerifyCodeResult {
  captchaEnabled: boolean
  uuid?: string
  img?: string
}

export interface LoginMobileParams {
  mobile: string
  code: string
  type: 'mobile'
}

export interface LoginResultModel {
  token: string
}

export function loginApi(params: LoginParams | LoginMobileParams) {
  return usePost<LoginResultModel, LoginParams | LoginMobileParams>('auth/login', params, {
    // 设置为false的时候不会携带token
    token: false,
    // 开发模式下使用自定义的接口
    customDev: true,
    // 是否开启全局请求loading
    loading: true,
  })
}

/**
 * 获取验证码
 */
export function getCodeImg(): AxiosPromise<VerifyCodeResult> {
  return request({
    url: '/auth/code',
    headers: {
      isToken: false,
    },
    method: 'get',
    timeout: 20000,
  })
}

export function logoutApi() {
  return useGet('/logout')
}
